<template>
  <el-card class="sku-list">
    <el-table border :data="skuList" style="width: 100%" v-loading="loading">
      <el-table-column type="index" align="center" label="序号" width="80">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column prop="prop" align="center" label="默认图片" width="150">
        <template slot-scope="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt="x"
            style="width: 100px; height: 100px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="width">
      </el-table-column>
      <el-table-column prop="price" label="价格(元)" width="80">
      </el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="{ row, $index }">
          <HintButton
            v-if="row.isSale == false"
            @click="onSale(row.id)"
            title="上架"
            type="info"
            icon="el-icon-top"
            size="mini"
          ></HintButton>
          <HintButton
            v-if="row.isSale == true"
            @click="cancelSale(row.id)"
            title="下架"
            type="success"
            icon="el-icon-bottom"
            size="mini"
          ></HintButton>
          <HintButton
            @click="toUpdateSku(row.id)"
            title="修改"
            type="primary"
            icon="el-icon-edit"
            size="mini"
          ></HintButton>
          <HintButton
            @click="showSkuInfo(row.id)"
            title="查看详情"
            type="info"
            icon="el-icon-info"
            size="mini"
          ></HintButton>

          <el-popconfirm
            :title="`确定删除${row.skuName}吗？`"
            @onConfirm="deleteSku(row.id)"
          >
            <HintButton
              slot="reference"
              title="删除"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></HintButton>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <!--  
         -->
    <el-pagination
      style="text-align: center"
      layout=" prev, pager, next, jumper, ->,sizes,total"
      :page-sizes="[5, 10, 20, 30, 40, 50]"
      :page-count="5"
      :current-page="page"
      :page-size="limit"
      :total="total"
      @current-change="getSkuList"
      @size-change="changeSize"
    >
    </el-pagination>

    <!-- 抽屉 -->
    <!-- :visible.sync控制显示与隐藏  :with-header="false"要不要头部  size="50%"占视口多大范围 -->
    <!-- gutter栅格间隔  span栅格占据的列数  el-carousel跑马灯  trigger指示器的触发方式-->
    <el-drawer :visible.sync="isShowSkuInfo" :with-header="false" size="50%" >
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="18">
          <el-tag
            type="success"
            style="margin-right: 5px"
            v-for="(AttrValue, index) in skuInfo.skuAttrValueList"
            :key="AttrValue.id"
          >
            {{ AttrValue.attrId + "-" + AttrValue.valueId }}
          </el-tag>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel class="sku-carousel"  trigger="click" height="400px">
            <el-carousel-item v-for="item in  skuInfo.skuImageList" :key="item.id">
                <img :src="item.imgUrl" alt="x" >
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>

<script>
export default {
  name: "SkuList",
  data() {
    return {
      loading: false, //是否正在加载
      skuList: [], //SKU列表
      total: 0, //数据库中的总记录条数
      page: 1,
      limit: 10, //每页显示几条

      isShowSkuInfo: false, //是否显示抽屉
      skuInfo: {}, //请求回来的sku详情信息
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    //异步获取指定页码的sku列表
    async getSkuList(page = 1) {
      this.page = page;
      this.loading = true;
      try {
        const result = await this.$API.sku.getList(this.page, this.limit);
        if (result.code === 200 || result.code === 20000) {
          this.skuList = result.data.records;
          this.total = result.data.total;
        } else {
          this.$message.error("获取SKU列表失败");
        }
      } catch (error) {
        this.$message.error("请求获取SKU列表失败");
      }
      this.loading = false;
    },

    //修改每页显示几条数据
    changeSize(size) {
      this.limit = size;
      this.getSkuList(1);
    },

    //对指定SKU进行上架的请求   onSale (skuId)
    async onSale(skuId) {
      this.loading = true;
      try {
        const result = await this.$API.sku.onSale(skuId);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("上架成功");
          this.getSkuList(this.page);
        } else {
          this.$message.error("上架失败");
        }
      } catch (error) {
        this.$message.error("请求上架失败");
      }
      this.loading = false;
    },

    //对指定SKU进行下架的请求 cancelSale (skuId)
    async cancelSale(skuId) {
      this.loading = true;
      try {
        const result = await this.$API.sku.cancelSale(skuId);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("下架成功");
          this.getSkuList(this.page);
        } else {
          this.$message.error("下架失败");
        }
      } catch (error) {
        this.$message.error("请求下架失败");
      }
      this.loading = false;
    },

    //修改sku，没有接口
    toUpdateSku(skuId) {
      this.$message.warning("正在开发中");
    },

    //删除sku    remove (skuId)
    async deleteSku(skuId) {
      this.loading = true;
      try {
        const result = await this.$API.sku.remove(skuId);
        if (result.code === 200 || result.code === 20000) {
          this.$message.success("删除SKU成功");
          this.getSkuList(this.page ? this.page : this.page - 1);
        } else {
          this.$message.error("删除SKU失败");
        }
      } catch (error) {
        this.$message.error("请求删除SKU失败");
      }
      this.loading = false;
    },

    //点击'查看详情'弹出抽屉  get (skuId)
    async showSkuInfo(skuId) {
      this.isShowSkuInfo = true;
      this.loading = true;
      try {
        const result = await this.$API.sku.get(skuId);
        if (result.code === 200 || result.code === 20000) {
          this.skuInfo = result.data;
        } else {
          this.$message.error("获取sku详情失败");
        }
      } catch (error) {
        this.$message.error("请求获取sku详情失败");
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.sku-list {
  .el-row {
    height: 40px;
    .el-col {
      line-height: 40px;
      &.el-col-5 {
        font-size: 18px;
        font-weight: bold;
        text-align: right;
        margin-right: 25px;
      }
    }
  }

  .sku-carousel {
    margin-top: 10px;
    width: 450px;
    border: 1px solid #ccc;
    img {
      width: 450px;
      height: 400px;
    }

    ::v-deep .el-carousel__indicator {
      button {
        display: inline-block;
        width: 15px;
        height: 15px;
        border-radius: 50%;
        margin-right: 10px;
        background-color: #666;
      }
      &.is-active {
        button {
          background-color: orangered;
        }
      }
    }
  }
}
</style>